import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:shared_preferences/shared_preferences.dart';
import 'package:xianyu_school/xianyuApp/following/followingPage.dart';
import 'package:xianyu_school/xianyuApp/mine/minePage.dart';
import 'home/homePage.dart';
import 'login/loginPage.dart';
import 'message/messagePage.dart';
import 'post/postPage.dart';

class xianyu extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: "Fish",
      home: FishHome(),
      routes: {
        "/home": (BuildContext context) => FishHome()
      },
    );
  }
}

class FishHome extends StatefulWidget{
  @override
  State<StatefulWidget> createState() {
    return _FishHomeState();
  }
}

class _FishHomeState extends State<FishHome>{


  String username;
  int _tabIndex = 0;
  PageController _pageController;
  bool LogFlag = false;

  @override
  void initState(){
    super.initState();
    _pageController = PageController(initialPage:_tabIndex,keepPage: true);
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      body: PageView(//实现页面滑动效果
        controller: _pageController,
        children: _pages,
        physics: NeverScrollableScrollPhysics(),
      ),
      floatingActionButton: circleButton(),//发布按钮
      floatingActionButtonLocation: FloatingActionButtonLocation.centerDocked,
      bottomNavigationBar: CupertinoTabBar(//底部导航栏
        backgroundColor: Colors.white,
        inactiveColor: normalColor,
        activeColor: normalColor,
        items: _bottomNaviItems(),
        currentIndex: _tabIndex,//当前页面
        onTap: didSelectedIndex,//点击之后跳转页面
      ),
    );
  }

  didSelectedIndex(int index) async {
    if(index == 0){
      setState(() {
        _tabIndex = index;
      });
      _pageController.jumpToPage(index);
    }
    else {
      SharedPreferences prefs = await SharedPreferences.getInstance();
      String username = prefs.getString("username");
      if (username == null) {
        Navigator.push<String>(context, MaterialPageRoute(builder: (_) {
          return loginPage();
        }));
      }
      else {
        setState(() {
          _tabIndex = index;
        });
        _pageController.jumpToPage(index);
      }
    }
  }

  void didTapPost() async {
    SharedPreferences prefs = await SharedPreferences.getInstance();
    String username = prefs.getString("username");
    if (!(username==null))
      Navigator.push(context, MaterialPageRoute(builder: (_) {
        return postPage();
      }));
    else
      Navigator.push(context, MaterialPageRoute(builder: (_) {
        return loginPage();
      }));
  }

  Widget circleButton() {
    return Padding(
      padding: EdgeInsets.only(top: 10),
      child: Container(
        width: 60,
        height: 60,
        decoration: BoxDecoration(
          color: Colors.white,
          shape: BoxShape.circle,
        ),
        child: Padding(
          padding: EdgeInsets.all(6),
          child: FlatButton(
            padding: EdgeInsets.only(right: 0),   // 修正child偏移量，居中
            //highlightColor: Colors.transparent, // 取消高亮
            splashColor: Colors.transparent,      // 取消水波
            shape: CircleBorder(),
            color: Colors.yellow,
            child: Icon(
              Icons.add,
              color: Colors.black,
              size: 40,
            ),
            onPressed: () {
              didTapPost();
            },
          ),
        ),
      ),
    );
  }

  List<StatefulWidget> _pages = [
    FSHomePage(),
    followingPage(),
    postPage(),
    FSMessagePage(),  // 消息
    minePage(),//我的
  ];

  static final Color normalColor = Colors.black;
  static final Color selectColor = Colors.yellow;

  List<Icon> _tabImages = [
    Icon(Icons.home, size: 25.0, color: normalColor,),
    Icon(Icons.category, size: 25.0, color: normalColor,),
    Icon(null, size: 25.0, color: normalColor,),
    Icon(Icons.notifications, size: 25.0, color: normalColor,),
    Icon(Icons.person, size: 25.0, color: normalColor,),
  ];

  List<Icon> _tabSelectedImages = [
    Icon(Icons.home, size: 25.0, color: selectColor,),
    Icon(Icons.category, size: 25.0, color: selectColor,),
    Icon(null, size: 25.0, color: selectColor,),
    Icon(Icons.notifications, size: 25.0, color: selectColor,),
    Icon(Icons.person, size: 25.0, color: selectColor,),
  ];

  // BottomNaviItem
  List<BottomNavigationBarItem> _bottomNaviItems() {
    return [
      BottomNavigationBarItem(icon: getTabIcon(0), title: Text("首页")),
      BottomNavigationBarItem(icon: getTabIcon(1), title: Text('关注')),
      BottomNavigationBarItem(icon: getTabIcon(2), title: Text('发布')),
      BottomNavigationBarItem(icon: getTabIcon(3), title: Text('消息')),
      BottomNavigationBarItem(icon: getTabIcon(4), title: Text('我的')),
    ];
  }
  Icon getTabIcon(int index) {
    if (index == _tabIndex) {
      return _tabSelectedImages[index];
    } else {
      return _tabImages[index];
    }
  }
}